<?php
	/**
	 * Created by PhpStorm.
	 * User: mybook-lhp
	 * Date: 18/6/7
	 * Time: 上午10:14
	 */

	namespace app\store\logic;


	use EasyWeChat\Factory;

	class EasyWechat
	{
		static protected $Init   = null;
		static protected $Config = null;

		static public function Init($config = [])
		{
			if (static::$Init == null)
			{
				static::$Init = new  static();
				static::config($config);
			}

			return static::$Init;
		}

		static protected function config($config)
		{
			$config_ = [
				// 下面为可选项
				// 指定 API 调用返回结果的类型：array(default)/collection/object/raw/自定义类名
				'response_type' => 'array',

				'log' => [
					'level' => 'debug',
					'file'  => config('EasyWeChatLogPath'),
				],
			];
			self::$Config = array_merge($config_, $config);
			$error = [];
			!isset(self::$Config['app_id']) ? $error['app_id'] = 'APP_ID 不存在！' : true;
			!isset(self::$Config['secret']) ? $error['secret'] = 'APP_secret 不存在！' : true;
			!isset(self::$Config['mch_id']) ? $error['mch_id'] = '商户ID不存在！' : true;
			!isset(self::$Config['key']) ? $error['key'] = '商户密钥不存在！' : true;
			if (count($error) > 0)
			{
				$data['code'] = 200;
				$data['msg'] = '参数错误';
				$data['data'] = self::$Config;
				response($data, 200, [], 'json');
			}
			return self::$Config;
		}


		static public function miniapp()
		{
			return Factory::miniProgram(self::$Config);
		}
	}